<template>
  <el-dialog :title="dialogProps.title" :visible.sync="dialogProps.visible" width="560px"
    @open="handleDialogOpen()" v-loading="loading">
    <div slot="title" class="dialog-header">{{ dialogProps.title }}</div>
    <el-form :model="libModel"
      ref="libForm" label-width="80px" label-position="right" size="small">
      <el-form-item label="项目名称" prop="name">
        <el-col :span="16"><el-input  :maxlength="32" v-model="libModel.name" autofocus></el-input></el-col>
      </el-form-item>
      <el-form-item label="项目强度" prop="strength">
        <el-col :span="16"><el-input :maxlength="32" v-model="libModel.strength"></el-input></el-col>
      </el-form-item>
      <el-form-item label="运动时间" prop="movementTime">
        <el-col :span="16"><el-input  :maxlength="32" v-model="libModel.movementTime"></el-input></el-col>
      </el-form-item>
      <el-form-item label="适用性别" prop="applyToGender">
        <el-col :span="16">
          <el-select v-model="libModel.applyToGender">
              <el-option value="0" label="男"></el-option>
              <el-option value="1" label="女"></el-option>
              <el-option value="2" label="不限"></el-option>
          </el-select></el-col>
      </el-form-item>
      <el-form-item label="年龄下限" prop="minAge">
        <el-col :span="16">
         <el-input :span="16" v-model="libModel.minAge"></el-input>
          </el-col>
      </el-form-item>
      <el-form-item label="年龄上限" prop="maxAge">
        <el-col :span="16"><el-input :maxlength="32" v-model="libModel.maxAge"></el-input></el-col>
      </el-form-item>
      <el-form-item label="千步当量" prop="thousandStepEquivalent">
        <el-col :span="16"><el-input  :maxlength="32" v-model="libModel.thousandStepEquivalent" autofocus></el-input></el-col>
      </el-form-item>
      <el-form-item label="拉伸部位" prop="tensileParts">
        <el-col :span="16">
          <el-select v-model="libModel.tensileParts">
              <el-option label="上肢" value="上肢"></el-option>
              <el-option label="下肢" value="下肢"></el-option>
              <el-option label="腰背" value="腰背"></el-option>
          </el-select>
          </el-col>
      </el-form-item>
      <el-form-item label="动作方法" prop="actionMethod">
        <el-input type="textarea" v-model="libModel.actionMethod"></el-input>
      </el-form-item>
      <el-form-item label="注意事项" prop="notes">
         <el-input type="textarea" v-model="libModel.notes"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button size="small" @click="handleDialogClose()">取 消</el-button>
      <el-button type="primary" size="small" @click="submitForm('libForm')">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { libraryAdd, libraryUpd } from '@/api/yundongxiangku_tj'
export default {
  name: 'xmk-edit',
  data() {
    return {
      loading: false,
      libModel: {

      },
      dialogProps: {
        visible: false,
        action: '',
        title: ''
      }
      }
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          if(this.dialogProps.action === 'add') {
            this.doSavelib()
          } else {
            this.doUpdatelib()
          }
        } else {
          return false
        }
      });
    },
    doUpdatelib() {
      this.loading = true
      libraryUpd(this.libModel)
      .then(response => {
        this.loading = false
        this.dialogProps.visible = false
        this.$emit('save-finished')
      })
      .catch(error => {
        this.outputError(error)
      })
    },
    doSavelib() {
      this.loading = true
      libraryAdd(this.libModel)
      .then(response => {
        this.loading = false
        this.dialogProps.visible = false
        this.$emit('save-finished')
      })
      .catch(error => {
        this.outputError(error)
      })
    },
    handleDialogClose() {
      this.dialogProps.visible = false
    },
    handleDialogOpen() {
      this.$nextTick(() => {
        this.$refs['libForm'].clearValidate()
      })
    },
    outputError(error) {
      console.log(error.response ? error.response : error.message)
      this.loading = false
      this.$message({
        showClose: true,
        message: '出错了，请按F12查看浏览器日志。',
        type: 'error'
      })
    }
  },
  mounted: function() {
    this.$nextTick(() => {
      this.$on('openEditDialog', function(TSportsItemLibrary) {
        this.libModel=TSportsItemLibrary
        this.dialogProps.action = 'edit'
        this.dialogProps.title = '修改'
        this.dialogProps.visible = true
      })
      this.$on('openAddDialog', function() {
        this.dialogProps.action = 'add'
        this.dialogProps.title = '添加'
        this.dialogProps.visible = true
      })
    })
  }
}
</script>

